package com.qfedu.orderadd.service.Impl;

import com.qfedu.fmmall.entity.Orders;
import com.qfedu.fmmall.entity.ShoppingCartVo;
import com.qfedu.orderadd.dao.OrderMapper;
import com.qfedu.orderadd.service.OrderAddService;
import com.qfedu.orderadd.service.feign.StockQueryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderAddServiceImpl implements OrderAddService {

    @Autowired
    private StockQueryClient stockQueryClient;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<ShoppingCartVo> save(Orders order, String cids) {
        //1.校验库存
        List<ShoppingCartVo> list = stockQueryClient.query(cids);
        if(list != null && list.size() >0){
            boolean f = true;
            String untitld = "";
            for(ShoppingCartVo sc : list){
                if(Integer.parseInt(sc.getCartNum()) > sc.getSkuStock()){ //当前数量是否大于库存
                    f = false;
                    break;
                }
                untitld += sc.getProductName()+",";
            }
            if(f){
                //库存充足
                //保存订单
                order.setUntitled(untitld);
                order.setCreateTime(new Date());
                order.setStatus("1");

                int i = orderMapper.insert(order);
                if(i>0){
                    return list;
                }
            }
        }
        return null;
    }
}
